package team.seekers.catering.community.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.catering.community.entity.dao.SecondComment;
import team.seekers.catering.community.entity.dto.add.SecondCommentAddDTO;
import team.seekers.catering.community.entity.dto.update.SecondCommentUpdateDTO;
import team.seekers.catering.community.entity.vo.info.SecondCommentInfoVO;
import team.seekers.catering.community.mapper.SecondCommentMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SecondCommentService extends ServiceImpl<SecondCommentMapper, SecondComment> {
    /**
     * 增加一个二级评论
     */
    public String add(SecondCommentAddDTO secondCommentAddDTO){
        SecondComment secondComment = new SecondComment();
        BeanUtils.copyProperties(secondCommentAddDTO,secondComment);
        save(secondComment);
        return secondComment.getId();
    }

    /**
     * 删除一个二级评论
     */
    public Boolean delete(String id){
        boolean remove = removeById(id);
        return remove;
    }

    /**
     * 修改一个二级评论
     */
    public String update(SecondCommentUpdateDTO secondCommentUpdateDTO){
        SecondComment secondComment = new SecondComment();
        BeanUtils.copyProperties(secondCommentUpdateDTO,secondComment);
        updateById(secondComment);
        return secondComment.getId();
    }

    /**
     * 根据id查询一个二级评论
     */
    public SecondCommentInfoVO get(String id){
        SecondComment secondComment = getById(id);
        SecondCommentInfoVO secondCommentInfoVO = new SecondCommentInfoVO();
        BeanUtils.copyProperties(secondComment,secondCommentInfoVO);
        return secondCommentInfoVO;
    }

    /**
     * 分页查询所有的二级评论
     */
    public Map<String, Object> adminPageSecondComment(long current, long limit, QueryWrapper<SecondComment> wrapper, String id) {
        Page<SecondComment> page = new Page<>(current,limit);
        wrapper.orderByDesc("gmt_create").eq("first_id",id);
        page(page,wrapper);
        Long total = page.getTotal();
        if(page.getRecords() == null){
            return null;
        }
        System.out.println("获取的记录是"+page.getRecords());

        List<SecondCommentInfoVO> list = new ArrayList<>();
        for (SecondComment record : page.getRecords()) {
            SecondCommentInfoVO vo = get(record.getId());
            list.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    /**
     * 根据一级id查询所有二级评论
     */
    public List<SecondComment> queryAllCommentByFirst(String firstId){
        QueryWrapper<SecondComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create").eq("first_id",firstId);
        List<SecondComment> secondComments = list(wrapper);
        return secondComments;
    }
}
